package com.mytx.sso.controller;

import com.alibaba.fastjson.JSONObject;
import com.mytx.common.annotation.SerializedField;
import com.mytx.common.config.MytxConfig;
import com.mytx.common.pojo.CommonResult;
import com.mytx.mapper.MytxConfigMapper;
import com.mytx.pojo.MytxConfigEntiy;
import com.mytx.sso.MyCommandLineRunner;
import com.mytx.sso.utils.RefreshConfigUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 刷新配置文件信息
 */
@RestController
public class RefreshConfigController {
    private static final Logger logger = LoggerFactory.getLogger(RefreshConfigController.class);

    @Autowired
    private MytxConfigMapper mytxConfigMapper;

    /**
     * 刷新配置文件信息
     * @return
     */
    @RequestMapping("/refreshConfig")
    @SerializedField(encrypt = false)
    public CommonResult refreshConfig(){
        logger.info("mytx-user:去数据库读取配置文件信息...");
        List<MytxConfigEntiy> mytxConfigList = mytxConfigMapper.findByAll();
        refreshConfig(mytxConfigList);
        return CommonResult.ok();
    }

    @Async
    public void refreshConfig(List<MytxConfigEntiy> mytxConfigList) {
        RefreshConfigUtils.refreshUserConfig(mytxConfigList);
    }
}
